package com.ruoyi.cms.utils;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.safety.Whitelist;

import java.io.IOException;

/**
 * xss非法标签过滤，过滤html中的xss字符 工具类
 *
 * @author Linzhaoguan
 * @version V1.0
 * @date 2019年9月11日
 */
public class JsoupUtil {
    /**
     * 使用自带的basicWithImages 白名单
     * 允许的便签有a,b,blockquote,br,cite,code,dd,dl,dt,em,i,li,ol,p,pre,q,small,span,
     * strike,strong,sub,sup,u,ul,img
     * 以及a标签的href,img标签的src,align,alt,height,width,title属性
     */
    private static final Whitelist WHITELIST = Whitelist.basicWithImages();
    /**
     * 配置过滤化参数,不对代码进行格式化
     */
    private static final Document.OutputSettings OUTPUT_SETTINGS = new Document.OutputSettings().prettyPrint(false);

    static {
        // 富文本编辑时一些样式是使用style来进行实现的
        // 比如红色字体 style="color:red;"
        // 所以需要给所有标签添加style属性
        WHITELIST.addAttributes(":all", "style");
    }

    public static String clean(String content) {
        if (StringUtils.isNotBlank(content)) {
            content = content.trim();
        }
        return Jsoup.clean(content, "", WHITELIST, OUTPUT_SETTINGS);
    }

    public static String get4Json(String url) throws IOException {
        Connection.Response response = Jsoup
                .connect(url)
                .header("Accept", "*/*")
                .header("Accept-Encoding", "gzip, deflate")
                .header("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
                .header("Content-Type", "application/json;charset=UTF-8")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like " +
                        "Gecko) Chrome/79.0.3945.117 Safari/537.36")
                .header("host", "data.wxb.com")
                .header("Cookie", "aliyungf_tc=AQAAAPeSOlxixQcAiAnleCAg/i6l3hbg; " +
                        "PHPSESSID=8f4b611036875633fcfc9bd14068cb71; " +
                        "Hm_lvt_5859c7e2fd49a1739a0b0f5a28532d91=1589091530; RMU=3613334; " +
                        "RMT=3fadd78be5b7b8edb830f7ccd55ef72c; Hm_lpvt_5859c7e2fd49a1739a0b0f5a28532d91=1589092431")
                .timeout(10000).ignoreContentType(true).execute();
        return response.body();
    }

    public static String get4TtJson(String url) throws IOException {
        Connection.Response response = Jsoup
                .connect(url)
                .header("method", "GET")
                .header("Accept", "*/*")
                .header("Host", "m.dcdapp.com")
                .header("Accept-Encoding", "gzip, deflate")
                .header("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
                .header("Content-Type", "application/json;charset=UTF-8")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like " +
                        "Gecko) Chrome/79.0.3945.117 Safari/537.36")
                .timeout(10000).ignoreContentType(true).execute();
        return response.body();
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        String url = String.format(CoreConst.WXB_API_URL, "国际", 2);
        System.out.println(url);
        for (int i = 0; i < 10; i++) {
            String json = get4TtJson("https://m.dcdapp.com/motor/m/article/detail?group_id=6828509052792734220");
            System.out.println(json);
            Thread.sleep(10000);
        }
    }
}